package cn.edu.sgu.www.mhxysy.function.computer.score.impl;

import cn.edu.sgu.www.mhxysy.entity.fabao.Fabao;
import cn.edu.sgu.www.mhxysy.entity.fabao.FabaoCategory;
import cn.edu.sgu.www.mhxysy.entity.fabao.FabaoLiangyiAttribute;
import cn.edu.sgu.www.mhxysy.entity.fabao.FabaoLiangyiAttributeValue;
import cn.edu.sgu.www.mhxysy.enums.FabaoTypes;
import cn.edu.sgu.www.mhxysy.exception.GlobalException;
import cn.edu.sgu.www.mhxysy.function.computer.score.ScoreComputer;
import cn.edu.sgu.www.mhxysy.mapper.fabao.FabaoCategoryMapper;
import cn.edu.sgu.www.mhxysy.mapper.fabao.FabaoLiangyiAttributeMapper;
import cn.edu.sgu.www.mhxysy.mapper.fabao.FabaoLiangyiAttributeValueMapper;
import cn.edu.sgu.www.mhxysy.mapper.fabao.FabaoMapper;
import cn.edu.sgu.www.mhxysy.restful.ResponseCode;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 法宝评分计算器
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Component
public class FabaoScoreComputer implements ScoreComputer {

    private final FabaoMapper fabaoMapper;
    private final FabaoCategoryMapper fabaoCategoryMapper;
    private final FabaoLiangyiAttributeMapper fabaoLiangyiAttributeMapper;
    private final FabaoLiangyiAttributeValueMapper fabaoLiangyiAttributeValueMapper;

    @Autowired
    public FabaoScoreComputer(
            FabaoMapper fabaoMapper,
            FabaoCategoryMapper fabaoCategoryMapper,
            FabaoLiangyiAttributeMapper fabaoLiangyiAttributeMapper,
            FabaoLiangyiAttributeValueMapper fabaoLiangyiAttributeValueMapper) {
        this.fabaoMapper = fabaoMapper;
        this.fabaoCategoryMapper = fabaoCategoryMapper;
        this.fabaoLiangyiAttributeMapper = fabaoLiangyiAttributeMapper;
        this.fabaoLiangyiAttributeValueMapper = fabaoLiangyiAttributeValueMapper;
    }

    @Override
    public Integer apply(String fabaoId) {
        Fabao fabao = fabaoMapper.selectById(fabaoId);

        if (fabao == null) {
            throw new GlobalException(ResponseCode.CONFLICT, "操作失败，法宝不存在！");
        }

        /*
         * 法宝评分 = {法宝等级 × 12} + 两仪属性评分 + 四象属性评分
         */
        AtomicInteger score = new AtomicInteger();

        score.addAndGet(fabao.getGrade() * 12);

        // 查询法宝两仪属性
        QueryWrapper<FabaoLiangyiAttribute> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("fabao_id", fabaoId);

        // 查询角色法宝的所有两仪属性
        List<FabaoLiangyiAttribute> list = fabaoLiangyiAttributeMapper.selectList(queryWrapper);

        FabaoCategory fabaoCategory = fabaoCategoryMapper.selectById(fabao.getCategoryId());

        for (FabaoLiangyiAttribute attribute : list) {
            Integer valueId = attribute.getValueId();
            FabaoLiangyiAttributeValue attributeValue = fabaoLiangyiAttributeValueMapper.selectById(valueId);

            Integer valueScore = attributeValue.getScore();

            // 门派法宝的两仪属性评分是普通法宝的两倍
            if (FabaoTypes.MPFB.getValue().equals(fabaoCategory.getType())) {
                valueScore *= 2;
            }

            score.addAndGet(valueScore);
        }

        return score.intValue();
    }

}